/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushserver;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author Filip
 */
public class Listener extends Thread{
    private Socket socket;
    private CommandHandler handler;

    public Listener(Socket socket, CommandHandler handler) {
        this.handler = handler;
        this.socket=socket;
    }

    @Override
    public void run(){
        try {
            /*byte[] data = new byte[4096];
            int read = di.read(data);
            if(read%2==0){

            }
            byte[] str = new byte[read];
            System.arraycopy(data, 0, str, 0, read);*/

            DataInputStream di = new DataInputStream(socket.getInputStream());
            String command = di.readUTF();
            handler.runCommand(command, socket.getInetAddress().getHostAddress());
            di.close();
            socket.close();

        } catch (EOFException eof){
            try{
                socket.close();
            }catch (IOException ex){
                handler.runCommand("balloon:Socket exception: "+ex.getMessage(), "127.0.0.1");
            }
        }catch (IOException ex) {
            handler.runCommand("balloon:Socket exception: "+ex.getMessage(), "127.0.0.1");
        }
    }
}
